﻿using System;
using System.Collections;
using System.Collections.Generic;
using com.guojin.mj.net.message.game;
using UnityEngine;
using UnityEngine.UI;

public class ChapterEndItemPage : EventManager
{//结算面板每个玩家的情况
    public override void InformationSetting()
    {
    }
    //如果是弃牌前两张显示背面  本玩家是弃牌看不到别人的牌，和分    当前玩家弃牌看不到自己的牌
    //如果当前局是特殊局。超过两个人完成游戏。显示所有人的分数。
    public Text nameTex;
    public GameObject groupObj;
    public Text cardTypeTex;
    public Text scoreTex;
    private int userState;//本条数据玩家的状态
    private int localState;//自己的状态

    public void InitChapterEndItem(UserChapterInfoRet ucir)
    {
        localState = GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].BetState;//当前玩家的状态

        userState = ucir.BetState;
        SetName(GameRoomModel.GetInstance().playerDic[ucir.LocationIndex].userName);

        SetCardGroup(ucir.Cards);
        SetCardType(ucir);
        SetScore(ucir.SettlementScore);
    }

    private void SetName(string name)
    {
        SetText(nameTex, name);
    }

    private void SetCardGroup(int[] cards)
    {
        if (GameRoomModel.GetInstance().isfinishGame)//如果是完成了游戏有两个人的状态是0 无状态 显示所有人的所有牌所有分数
        {
            for (int i = 0; i < cards.Length; i++)
            {
                if (userState == 5 && i < 2)//游戏结束了，词条游戏玩家的牌是弃牌就不良出来
                {
                    groupObj.transform.GetChild(i).GetComponent<ChapterEndCard>().LoadBackSprite();
                }
                else
                {
                    groupObj.transform.GetChild(i).GetComponent<ChapterEndCard>().cardValue = cards[i];
                    groupObj.transform.GetChild(i).GetComponent<ChapterEndCard>().LoadValueSprite();
                }
                groupObj.transform.GetChild(i).gameObject.SetActive(true);

            }
        }
        else//不是正常完成的游戏，所有人扣两张牌
        {
            for (int i = 0; i < cards.Length; i++)//游戏结束了，词条游戏玩家的牌是弃牌就不良出来
            {
                //int localState=        GameRoomModel.GetInstance().playerDic[0].BetState;//当前玩家的状态
                //            Debug.LogError("mianban xianshi de 手牌" + localState);

        //if (userState == 5 && i < 2)//游戏结束了，词条游戏玩家的牌是弃牌就不良出来
                if ((localState == 5 || userState == 5) && i < 2)//自己是弃牌，自己和别人都看不到自己的牌
                {
                    groupObj.transform.GetChild(i).GetComponent<ChapterEndCard>().LoadBackSprite();
                }
                else
                {
                    groupObj.transform.GetChild(i).GetComponent<ChapterEndCard>().cardValue = cards[i];
                    groupObj.transform.GetChild(i).GetComponent<ChapterEndCard>().LoadValueSprite();
                }

                groupObj.transform.GetChild(i).gameObject.SetActive(true);
            }
        }

    }

    private void SetCardType(UserChapterInfoRet ucir)
    {

        //Debug.Log(GameRoomModel.GetInstance().playerDic[ucir.LocationIndex].AllCardType);
        if (userState != 5)
        {
            if (localState != 5)
                SetText(cardTypeTex, ucir.AllCardType);
            else
                //SetText(cardTypeTex, GameRoomModel.GetInstance().playerDic[ucir.LocationIndex].ShowCardType);
                SetText(cardTypeTex, ucir.ShowCardType);
        }
        else
        {
            SetText(cardTypeTex, "弃牌");
        }
    }

    private void SetScore(int score)
    {
        string totalScore = score.ToString();

        if (totalScore.Substring(0, 1) == "-")
        {
            SetText(scoreTex, string.Format("<color=red>{0}</color>", totalScore));
        }
        else
        {
            SetText(scoreTex, string.Format("<color=green>+{0}</color>", totalScore));
        }
    }
}
